/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef _I2CH_
#define _I2CH_

#ifdef __cplusplus
extern "C" {
#endif

#include "atom.h"
#include "atommutex.h"
#include "iosdrv.h"

#define I2C_WR                0x0000
#define I2C_RD               (1u << 0)
#define I2C_ADDR_10BIT       (1u << 2)  /* this is a ten bit chip address */
#define I2C_NO_START         (1u << 4)
#define I2C_IGNORE_NACK      (1u << 5)
#define I2C_NO_READ_ACK      (1u << 6)  /* when I2C reading, we do not ACK */

#define I2C_DEV_CTRL_10BIT        0x20
#define I2C_DEV_CTRL_ADDR         0x21
#define I2C_DEV_CTRL_TIMEOUT      0x22
#define I2C_DEV_CTRL_RW           0x23

struct i2c_msg
{
    uint16_t addr;
    uint16_t flags;
    uint16_t len;
    uint8_t  *buf;
};

typedef struct I2c I2c;


typedef struct _i2cOps {
    size_t (*master_xfer)(I2c *bus, struct i2c_msg msgs[], uint32_t num);
    size_t (*slave_xfer)(I2c *bus, struct i2c_msg msgs[], uint32_t num);
}I2cOps;


struct I2c {
    IOSDrv super;
    struct {
        I2cOps* ops;
        ATOM_MUTEX lock;
        uint16_t  flags;
        uint16_t  addr;
        uint32_t  timeout;
        uint32_t  retries;
        void* priv;
    }Protected;
};

struct I2cData
{
    struct i2c_msg  *msgs;
    size_t  number;
};

void I2cCreate(I2c* i2c, const char *name);

size_t I2cTransfer(I2c *bus, struct i2c_msg msgs[], uint32_t num);
size_t I2cMasterSend(I2c* bus, uint16_t addr, uint16_t flags, const uint8_t* buf, uint32_t count);
size_t I2cMasterRecv(I2c* bus, uint16_t addr, uint16_t flags, uint8_t* buf, uint32_t count);

#ifdef __cplusplus
}
#endif

#endif  /* _ADCH_ */

